package com.lams.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

public class Children {
	private List<TreeNode> list = new ArrayList<TreeNode>();
	
	public int getSize(){
		return list.size();
	}
	
	public void addChild(TreeNode node){
		list.add(node);
	}
	
	public String toString(){
		String result = "[";
		for(Iterator it = list.iterator();it.hasNext();){
			result += ((TreeNode)it.next()).toString();
			result += ",";
		}
		result = result.substring(0,result.length()-1);
		result += "]";
		return result;
	}
	
	public void sortChildren(){
		Collections.sort(list, new NodeIdComparator());
		for(Iterator it = list.iterator();it.hasNext();){
			((TreeNode)it.next()).sortChildren();
		}
	}
	
	class NodeIdComparator implements Comparator {

		@Override
		public int compare(Object o1, Object o2) {
			int n1 = Integer.parseInt(((TreeNode)o1).id);
			int n2 = Integer.parseInt(((TreeNode)o2).id);
			return n1<n2 ? -1 : n1==n2 ? 0 : 1;
		}
		
	}
}
